package com.situ.web.util;

// JSON格式的通用响应对象封装的就是后台返回给前台的所有信息
public class JSONResult {
	private static final int ERROR = 1;
	private static final int SUCCESS = 0;

	// 当前状态（程序员判断状态）:成功、失败、未登录、没有权限
	// 当前登录是成功还是失败要告诉前台，前台才能知道弹出的提示框用errorMsg、successMsg
	private Integer code;
	// 描述信息（主要是给用户看的提示信息）
	private String msg;
	// 总数量（按理说是不需要，主要是给LayUI table）
	private Integer count;
	// 后台返回给前端的数据 Object， User、List<User>
	private Object data;
	
	public boolean isSuccess() {
		return code == SUCCESS;
	}

	public JSONResult() {
	}
	
	public JSONResult(Integer code) {
		this.code = code;
	}

	public JSONResult(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	
	public JSONResult(Integer code, Object data) {
		this.code = code;
		this.data = data;
	}

	public JSONResult(Integer code, String msg, Object data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}
	
	public JSONResult(Integer code, String msg, Object data, Integer count) {
		this.code = code;
		this.msg = msg;
		this.data = data;
		this.count = count;
	}

	// 告诉前台成功：code
	public static JSONResult success() {
		return new JSONResult(SUCCESS);
	}
	
	// 告诉前台成功：code、msg
	public static JSONResult success(String msg) {
		return new JSONResult(SUCCESS, msg);
	}
	
	// 告诉前台成功：code、data
	public static JSONResult success(Object data) {
		return new JSONResult(SUCCESS, data);
	}
	
	// 告诉前台成功：code、msg、data
	public static JSONResult success(String msg, Object data) {
		return new JSONResult(SUCCESS, msg, data);
	}
	
	// 告诉前台成功：code、msg、data、count
	public static JSONResult success(String msg, Object data, Integer count) {
		return new JSONResult(SUCCESS, msg, data, count);
	}

	// 告诉前台成功：code
	public static JSONResult error() {
		return new JSONResult(ERROR);
	}
	
	// 告诉前台成功：code、msg
	public static JSONResult error(String msg) {
		return new JSONResult(ERROR, msg);
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
	
}
